﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ue.Framework.Web.WebControls;
using Ue.Infrastructure.Web.UI;
using Ue.Qcdz.Domain.Permissions;
using Ue.Qcdz.Domain.Roles;
using Ue.Qcdz.Domain.TheResource;

namespace Ue.Qcdz.UI.Controls.Roles
{
    public class RolesEdit : SkinnedEntityEdit<Role>
    {
        private TextBox tbxName;
        private CheckBoxList chkPermission;
        private RadioButtonList rdbResource;
        private string sinkName = "~/Admin/Themes/Skins/Roles/RolesEdit.ascx";
        protected override string GetSkinFileName()
        {
            return sinkName;
        }
        private void BindCheck()
        {
            IList<Ue.Qcdz.Domain.Permissions.Permission> permissionList = PermissionsService.GetPermissions();
            foreach (Domain.Permissions.Permission permission in permissionList)
            {
                ListItem item = new ListItem(permission.Name, permission.Key.ToString());
                chkPermission.Items.Add(item);
            }
        }
        private void BindResource()
        {
            IList<Resource> resourceList = ResourceService.GetResource();
            foreach (Resource resource in resourceList)
            {
                StringBuilder stringBuilder=new StringBuilder();
                stringBuilder.Append(resource.Name);
                stringBuilder.Append("(");
                stringBuilder.Append(resource.Num);
                stringBuilder.Append(")");
                ListItem item = new ListItem(stringBuilder.ToString(), resource.Key.ToString());
                rdbResource.Items.Add(item);
            }
        }

        /// <summary>
        /// 绑定选择的权限列表
        /// </summary>
        /// <returns></returns>
        private List<string> GetPermissionCheck()
        {
            List<string> permissionList = new List<string>();
            if (chkPermission != null)
            {
                for (int i = 0; i < chkPermission.Items.Count; i++)
                {
                    if (chkPermission.Items[i].Selected)
                    {
                        permissionList.Add(chkPermission.Items[i].Value);
                    }
                }
                return permissionList;
            }
            return null;
        }
        /// <summary>
        /// 绑定选择的资源列表
        /// </summary>
        /// <returns></returns>
        private List<string> GetResourceCheck()
        {
            List<string> resourceList = new List<string>();
            if (chkPermission != null)
            {
                for (int i = 0; i < rdbResource.Items.Count; i++)
                {
                    if (rdbResource.Items[i].Selected)
                    {
                        resourceList.Add(rdbResource.Items[i].Value);
                    }
                }
                return resourceList;
            }
            return null;
        }
        /// <summary>
        /// CheckListBox的选中效果
        /// </summary>
        /// <param name="role"></param>
        private void GetCheckResource(Role role)
        {
            if (role.ResourceList.Count != 0)
            {
                foreach (var resource in role.ResourceList)
                {
                    for (int i = 0; i < rdbResource.Items.Count; i++)
                    {
                        if (rdbResource.Items[i].Value.Equals(resource.Key.ToString()))
                        {
                            rdbResource.Items[i].Selected = true;
                        }
                    }
                }
            }
        }
        /// <summary>
        /// CheckListBox的选中效果
        /// </summary>
        /// <param name="role"></param>
        private void GetCheck(Role role)
        {
            if (role.PermissionList.Count != 0)
            {
                foreach (var permission in role.PermissionList)
                {
                    for (int i = 0; i < chkPermission.Items.Count; i++)
                    {
                        if (chkPermission.Items[i].Value.Equals(permission.Key.ToString()))
                        {
                            chkPermission.Items[i].Selected = true;
                        }
                    }
                }
            }
        }

        protected override Role Enttiy
        {
            get
            {
                var role = new Role(
                    tbxName.GetValue<string>(),
                    this.EntityKey
                    );

                List<string> permissionIdList = GetPermissionCheck();
                List<string> resourceIdList = GetResourceCheck();
                if (permissionIdList != null)
                {
                    var permissionList =
                        permissionIdList.Select(id => new Domain.Permissions.Permission(null, null, null, id)).ToList();
                    role.ModifyPermission(permissionList);
                    
                }
                if (resourceIdList!=null)
                {
                    var resourceList = resourceIdList.Select(id => new Resource(id)).ToList();
                    role.ModifyResource(resourceList);
                }
                return role;
            }
            set
            {
                if (value == null)
                    return;

                tbxName.SetValue(value.Name);
                GetCheck(value);
                GetCheckResource(value);
            }
        }

        protected override void InitSkin(Control skin)
        {

            tbxName = skin.FindControl<TextBox>("tbxName");
           
            chkPermission = skin.FindControl<CheckBoxList>("chkPermission");
            rdbResource = skin.FindControl<RadioButtonList>("rdbResource");
            if (chkPermission != null)
            {
                BindCheck();
            }
            if (rdbResource != null)
            {
                BindResource();
            }
        }
    }
}
